package my;

import java.net.URLEncoder;
import java.sql.Timestamp;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Locale;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import debug.ConfigSettings;

public class Game {
	static String CLASS_NAME;
	static String TAG_NAME;
	static String CLASS_PRICE;
	static String CLASS_IMAGE;
	static String CLASS_DISCOUNT;
	static ConfigSettings settings;
	
	public Game() {};
	/**
	 * Constructs a game object
	 * @param e element containg the game
	 * @param settings 
	 * @throws Exception
	 */
	public Game( Element e, ConfigSettings settings ) throws Exception {
		CLASS_NAME = settings.get("SEARCH_NAME");
		TAG_NAME = settings.get("NAME_TAG");
		CLASS_PRICE = settings.get("TAB_PRICE");
		CLASS_IMAGE = settings.get("IMAGE_URL");
		CLASS_DISCOUNT = settings.get("DISCOUNT");
		Game.settings = settings;
		SetName(GetClassName(e));
		SetPrice(GetClassPrice(e));
		SetImageURL(GetImage(e));
		SetDiscount(GetDiscount(e));
		SetHrefURL(GetHrefURL(e));
		ctime = new Timestamp(new Date().getTime());
	}
	public Game( String name, double price, long ctime) {
		SetName(name);
		this.price = price;
		this.ctime = new Timestamp(ctime);
	}
	public Game( String name, String price) throws Exception {
		SetName(name);
		SetPrice(price);
	}
	
	public boolean Equals( Game r ) {
		if( name == r.name && price == r.price ) {
			return true;
		}
		return false;
	}
	
	static public String GetImage( Element e ) throws Exception {
		Elements links;
		if( !(links = e.getElementsByClass(CLASS_IMAGE)).isEmpty()) {
				return links.get(0).html();
		}
		else {
			throw new Exception("Element missing " + CLASS_IMAGE+ " Class");
		}
	}
	static public String GetDiscount( Element e ) throws Exception {
		Elements links;
		if( !(links = e.getElementsByClass(CLASS_DISCOUNT)).isEmpty()) {
				return links.get(0).text();
		}
		else {
			throw new Exception("Element missing " + CLASS_IMAGE+ " Class");
		}
	}
	static public String GetClassName( Element e ) throws Exception {
		Elements links;
		if( !(links = e.getElementsByClass(CLASS_NAME)).isEmpty()) {
			return links.get(0).getElementsByTag(TAG_NAME).text();
		}
		else {
			throw new Exception("Element missing " + CLASS_NAME + " Class");
		}
	}
	static public String GetClassPrice( Element e ) throws Exception {
		Elements links;
		if( !(links = e.getElementsByClass(CLASS_PRICE)).isEmpty()) {
			String temp [] = links.get(0).text().split(" ");
			return temp[1];
		}
		else {
			throw new Exception("Element missing " + CLASS_PRICE + " Class");
		}
	}
	
	static public String GetHrefURL( Element e ) throws Exception {
		Elements links;
		if( !(links = e.getElementsByTag("a")).isEmpty()) {
			return links.get(0).attr("href");
		}
		else {
			throw new Exception("Element missing href Class");
		}
	}
	public void SetHrefURL( String url ) {
		this.hrefURL = url;
	}
	public void SetPrice( String price ) throws Exception {
		if( price.isEmpty() || price == null) {
			this.price = 0;
		}
		else {
			NumberFormat cf = NumberFormat.getCurrencyInstance(new Locale(settings.get("LANGUAGE"), settings.get("COUNTRY")));		
			this.price = cf.parse(price).doubleValue();
		}
	}
	public void SetName( String name ) {
		this.name = name;
	}
	public void SetDiscount( String pct ) {
		this.discount_pct = pct;
	}
	public void SetImageURL( String url ) {
		this.image_URL = url;
	}
	public void SetMetascore( String score ) {
		this.metascore = score;
	}
	
	public boolean GrabMetascore( ) throws Exception {
		Document doc;
		Elements top;
		Elements ele;
		
		if( (doc = Jsoup.connect("http://store.steampowered.com/search/?term="+ URLEncoder.encode(Name(), settings.get("ENCODING"))).get() ) == null ) {
			throw new Exception("Could not fetch Name()");
		}
		if( ( top = doc.getElementsByClass(settings.get("SEARCH_RESULT_CLASS"))).isEmpty() ) {
			throw new Exception("Could not fetch Search list");
		}
		for( Element e : top ) {
			if( (ele = e.getElementsByTag(settings.get("SEARCH_RESULT_NAME_TAG"))).isEmpty() ) {
				continue;
			}
			if( ele.text().equals(Name())) {
				if( ( ele = e.getElementsByClass(settings.get("SEARCH_METASCORE_CLASS"))).isEmpty() ) {
					throw new Exception("Game missing metascore");
				}
				this.SetMetascore(ele.text());
				return true;
			}
		}
		this.SetMetascore("");
		return false;
	}
	
	public double Price() { return price; }
	public String PriceLocale() throws Exception { 
		NumberFormat cf = NumberFormat.getCurrencyInstance(new Locale(settings.get("LANGUAGE"), settings.get("COUNTRY")));		
		return cf.format(Price());
	}
	
	public String Name() { return name; }
	public String Discount_Pct() { return discount_pct; }
	public String ImageURL() { return image_URL; }
	public Timestamp GetCTime() { return ctime; };
	public String HrefURL() { return hrefURL; }
	public String Metascore() { return metascore; }
	private String name;
	private double price;
	private String image_URL;
	private String discount_pct;
	private Timestamp ctime;
	private String hrefURL;
	private String metascore;
}
